<?php

class aoiItineraryReservationComponents extends sfComponents {
  public function executeSearchForm($request)
  {

    // Flights

    $this->originAirports = Doctrine_Core::getTable('AoiAirport')
                            ->createQuery('a')
                            ->execute();

    $destinationAirports = array();

    foreach($this->originAirports as $airport)
    {
      $destinationAirports[$airport->getCode()] = Array();
      foreach($airport->getFlightSchedules() as $schedule)
      {
        $destinationAirport = $schedule->getAoiDestinationAirport();
        $code = $destinationAirport->getCode();
        $name = $destinationAirport->getName();
        $destinationAirports[$airport->getCode()] []= array('code'=>$code, 'name'=>$name);
      }
    }
    $this->destinationAirports = $destinationAirports;

    $this->origin_airport      = $request->getParameter('origin_airport','');
    $this->destination_airport = $request->getParameter('destination_airport','');

    $now = time();
    $tomorrow = strtotime('+1 Day', $now);
    $this->date_go_str = date('d/m/Y', $tomorrow);
    $this->date_go = date('Y-m-d', $tomorrow);
    $nextWeek = strtotime('+1 Week', $now);
    $this->date_back_str = date('d/m/Y', $nextWeek);
    $this->date_back = date('Y-m-d', $nextWeek);

    // Passengers

    $passengerCategories = Doctrine_Core::getTable('AoiPassengerCategory')
                            ->createQuery('p')
                            ->execute();

    $this->passenger_categories = array();

    foreach($passengerCategories as $passengerCategory)
    {
      $this->passenger_categories []= array('name'=>$passengerCategory->getName(),'id'=>$passengerCategory->getId());
    }
  }

  public function executeBookingPassengerForm($request)
  {
    if(!isset($this->passenger_category_id)) $this->passenger_category_id = 0;
    if(!isset($this->index)) $this->index = 0;

    $this->passengerCategory = Doctrine_Core::getTable('AoiPassengerCategory')->findOneById($this->passenger_category_id);
  }

  public function executeBookingPassengerInfos($request)
  {
    if(!isset($this->index)) $this->index = 0;
    if(!isset($this->passenger_infos)) $this->passenger_infos = array();

    $this->passengerCategory = (isset($this->passenger_infos['category_id'])) ? Doctrine_Core::getTable('AoiPassengerCategory')->findOneById((int) $this->passenger_infos['category_id']) : false;
  }
  
}